package chinaums

import (
	"time"
	//"encoding/json"
	"thirdpaysdk/utils/mapstruct"
	//"github.com/astaxie/beego"
)

type RefundResponse struct {
	ErrCode             string  `json:"errCode"` 
	MsgSrc              string  `json:"msgSrc"`
	ResponseTimestamp   string  `json:"responseTimestamp"`
	Mid                 string  `json:"mid"`
	Tid                 string  `json:"tid"`
	//
	MsgType             string  `json:"msgType,omitempty"`
	//MsgId               string  `json:"msgId,omitempty"`
	//ErrMsg              string  `json:"errMsg,omitempty"` 
	//SrcReserve          string  `json:"srcReserve,omitempty"`
	MerOrderId          string  `json:"merOrderId,omitempty"`
	MerName             string  `json:"merName,omitempty"`
	SeqId               string  `json:"seqId,omitempty"` 
	Status              string  `json:"status,omitempty"`
	TargetMid           string  `json:"targetMid,omitempty"` 
	//TargetOrderId       string  `json:"targetOrderId,omitempty"` 
	TargetStatus        string  `json:"targetStatus,omitempty"` 
	TargetSys           string  `json:"targetSys,omitempty"`
	TotalAmount         int     `json:"totalAmount,omitempty"`
	//RefundAmount        int     `json:"refundAmount,omitempty"`
	RefundFunds         string  `json:"refundFunds,omitempty"`
	RefundFundsDesc     string  `json:"refundFundsDesc,omitempty"`
	//RefundInvoiceAmount int     `json:"refundInvoiceAmount,omitempty"` 
	RefundOrderId       string  `json:"refundOrderId,omitempty"`
	RefundTargetOrderId string  `json:"refundTargetOrderId,omitempty"`
	Sign                string  `json:"sign"`
}

/*
{"msgType":"wx.refund","payTime":"2019-04-12 21:17:51","connectSys":"UNIONPAY","merName":"北京大黄蜂","mid":"898440057340005","refundStatus":"SUCCESS","settleDate":"2019-04-12","tid":"99113921","refundTargetOrderId":"50100600112019041209011781529","refundFundsDesc":"现金退款0.01元。","refundFunds":"现金:1","targetMid":"281262020","cardAttr":"BALANCE","targetStatus":"SUCCESS|SUCCESS","seqId":"07282021040N","merOrderId":"501385038399655936","targetSys":"WXPay","bankInfo":"零钱","msgSrc":"WWW.BJDHFXX.COM","settleRefId":"07282012986N","refundOrderId":"10001904124481751215904948","totalAmount":1,"responseTimestamp":"2019-04-12 21:17:52","errCode":"SUCCESS","status":"TRADE_SUCCESS","sign":"9534159FF7FA362FDC356B5662B8121E"}
*/

func RefundPay(mid,tid,msgSrc,secreCode,orderId string,amount int)(rsp *RefundResponse, err error){
	type SubOrder struct {
		Mid         string  `json:"mid"`         //子商户号
		TotalAmount int     `json:"totalAmount"` //子商户分账金额
	}
	type Request struct {
	    MsgSrc           string  `json:"msgSrc"`
		MsgType          string  `json:"msgType"`
		RequestTimestamp string  `json:"requestTimestamp"`
		InstMid          string  `json:"instMid"`
		Mid              string  `json:"mid"`
		Tid              string  `json:"tid"`
		RefundAmount     int     `json:"refundAmount"`
		MerOrderId       string  `json:"merOrderId,omitempty"`
		PlatformAmount   int     `json:"platformAmount,omitempty"`
		SubOrders    []*SubOrder `json:"subOrders,omitempty"`
		Sign             string  `json:"sign"`
	}
	var request Request
	request.MsgSrc = msgSrc
	request.MsgType = "refund"
	request.RequestTimestamp = time.Now().Format("2006-01-02 15:04:05")
	request.InstMid = "MINIDEFAULT"
	request.Mid = mid
	request.Tid = tid
	request.RefundAmount = amount
	request.MerOrderId = orderId
	reqParams := mapstruct.Struct2Map(request, "json")
	reqURL := "https://qr.chinaums.com/netpay-route-server/api/"
	sdk := NewSDK(reqURL, secreCode)
	resp, e := sdk.RequestOther(reqParams)
	if e != nil {
	    err = e
		return
	}
	//beego.Debug("%v", resp)
	rsp = new(RefundResponse)
	err = mapstruct.Map2Struct(resp,rsp,"json")
	return
}
